<?php
namespace app\teacher\controller;
use app\common\Base;
use app\common\TeacherModel;
use think\Db;

class TeacherZczr extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$grade = input("grade");
		$name = input("teacherName");
		$schoolStatus = input("schoolStatus");
		$page = input("page");
		$rows = input("rows");
		$sort = input("sort");
		$order = input("order");
		$orderby = null;
		if($sort) {
			$orderby = $sort." ".$order;
		}
		$where = [];
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($name) $where["姓名"] = array("like", $name."%");
		if($schoolStatus) {
			$where["离校学期ID"] = array("eq", $schoolStatus);
		}
		$fields = ("ID,姓名,年级号,学籍类型,任教学科ID,手机号,离校学期ID,职位");
		$Model = new TeacherModel();
		return $Model->getTeacherList($page, $rows, $fields, $where,$orderby);
	}

	public function addUpdate() {
		try {
			$all = request()->param();
			if(array_key_exists("年级号", $all) && !empty($all["年级号"])) {
				if($all["年级号"] == "0") {
					$all["年级号"] = 0;
				}else {
					$array = explode("_", $all["年级号"]);
					$all["年级号"] = $array[0];
					$all["学籍类型"] = $array[1];
				}
			}
			if(array_key_exists("职位", $all) && !empty($all["职位"])) {
				$data = Db::table("t_organize_depart")->where("PARENT_ID > 0")->select();
				$array = explode(",", $all["职位"]);
			    $result = [];
			    foreach ($array as $value) {
			        foreach ($data as $obj) {
			        	if($obj["ID"] == $value && !in_array($obj["PARENT_ID"], $result)) {
							array_push($result, $obj["PARENT_ID"]);
							break;
			        	}
			        }
			    }
			    if(count($result) > 0) {
			    	$all["部门"] = implode(",", $result);
			    }
			}
			update('教师信息', $all);
			return '000';
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	} 

}
